package com.cee.admin.modules.pm.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.common.core.service.CommonService;
import com.cee.common.exception.GlobalException;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.pm.entity.PmConfig;
import com.cee.ds.modules.pm.service.IPmConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import com.cee.admin.common.BaseController;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.cee.cache.constants.CacheNamesConstants.PM_CONFIG_TYPE_CACHE;

/**
 * <p>
 * 门户配置表 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2024-04-23
 */
@Tag(name = "门户管理-门户配置")
@RestController
@RequestMapping("/api/pm/config")
@CacheConfig(cacheNames = PM_CONFIG_TYPE_CACHE)
public class PmConfigController extends BaseController {

    @Autowired
    private IPmConfigService iPmConfigService;

    @Autowired
    private CommonService commonService;

    @GetMapping("/page")
    @Operation(summary = "分页")
    public Result<Page<PmConfig>> page(@RequestParam("current") Long current, @RequestParam("size") Long size , @Valid PmConfig query) {
        return success(iPmConfigService.lambdaQuery().eq(PmConfig::getType, query.getType()).orderByAsc(PmConfig::getSort).page(new Page<>(current,size)));
    }

    @PostMapping("/save")
    @Operation(summary = "新增")
    @CacheEvict(key = "#query.type")
    @RequiresPermissions("pm:config:save")
    public Result<Boolean> save(@Valid @RequestBody PmConfig query) {
        return success(commonService.saveWithFile(iPmConfigService,query));
    }

    @PostMapping("/update")
    @Operation(summary = "修改")
    @CacheEvict(key = "#query.type")
    @RequiresPermissions("pm:config:update")
    public Result<Boolean> update(@RequestBody PmConfig query) {
        if (Optional.ofNullable(query.getContent()).orElse("").length()>1000){
            throw new GlobalException("内容的长度不得超过1000个字");
        }
        return success(commonService.updateWithFile(iPmConfigService,query,()->iPmConfigService.getById(query.getId())));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除")
    @RequiresPermissions("pm:config:delete")
    @CacheEvict(allEntries = true)
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return success(commonService.deleteWithFile(iPmConfigService,id));
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "详情")
    public Result<PmConfig> get(@PathVariable("id") Long id) {
        return success(iPmConfigService.getById(id));
    }

    @PostMapping("/service/submit")
    @Operation(summary = "提交服务简介、公司简介", description = "提交服务简介或公司简介,类型为6、7、8、9、10")
    @CacheEvict(key = "#query.type")
    @RequiresPermissions(value = {"pm:config:aboutUs:submit","pm:config:home:submit"}, logical = Logical.OR)
    public Result<Boolean> submitService(@Valid @RequestBody PmConfig query) {
        List<PmConfig> list = iPmConfigService.lambdaQuery().eq(PmConfig::getType, query.getType()).list();
        if (CollectionUtils.isNotEmpty(list)){
            PmConfig old = list.get(0);
            list.remove(0);
            commonService.deleteBatchWithFile(iPmConfigService,list.stream().map(PmConfig::getId).collect(Collectors.toList()));
            query.setId(old.getId());
            return success(commonService.updateWithFile(iPmConfigService,query,()-> old));
        }
        return success(commonService.saveWithFile(iPmConfigService,query));
    }

}
